package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.service.IDataSetService;
import com.yeshine.opendata.service.IDictionaryService;
import com.yeshine.opendata.service.IOrgService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * Created by taohj on 2014/7/7.
 */

@Controller
@RequestMapping("admin/org")
public class AdminOrgController extends AbstractAdminControllerBase {


    @Autowired
    private IOrgService service;



    @Autowired
    private IDataSetService dataSetService;

    @Autowired
    private IDictionaryService dictionaryService;


    @RequestMapping(value = "createV", method = RequestMethod.GET)
    public String createV() {
        Result<Org> result = service.getList(null);
        setRequestAttr("result", result);
        SysDictionary sysDictionary = new SysDictionary();
        sysDictionary.setType("ORG_CATEGORY");
        setRequestAttr("categorys", dictionaryService.getList(sysDictionary).getList());
        return "admin/org/create";
    }


    @ResponseBody
    @RequestMapping(value = "create")
    public String create(
            @RequestParam("name") String name,
            @RequestParam("superOrg") Integer superOrg,
            @RequestParam("image") MultipartFile image,
            @RequestParam("category") Integer category,
            @RequestParam("contacts") String contacts,
            @RequestParam("telephone") String telephone,
            @RequestParam("email") String email,
            @RequestParam("address") String address,
            @RequestParam("postcode") String postcode,
            @RequestParam("description") String description
    ) {
        Org org = new Org();
        org.setName(name);
        org.setHeadImageFile(image);
        org.setCategory(new SysDictionary(category));
        org.setContacts(contacts);
        org.setTelephone(telephone);
        org.setEmail(email);
        org.setAddress(address);
        org.setPostcode(postcode);
        org.setStatus(new SysDictionary(183));
        org.setCreater(null);
        org.setCreateTime(new Date());
        if (superOrg != null) {
            org.setSuperOrg(new Org(superOrg));
        }

        org.setDescription(description);
        Result<Org> result = service.create(org, getRequest());

        result.setMessage(result.isSuccess() ? "新增成功" : result.getMessage());
        return result.toString();
    }


    @ResponseBody
    @RequestMapping("remove")
    public String remove(@RequestParam("id") Integer id) {
        Result<Org> result = service.remove(new Org(id));
        result.setMessage(result.isSuccess() ? "删除成功" : result.getMessage());
        return result.toString();
    }


    @RequestMapping(value = "modifyV")
    public String modifyV(@RequestParam("id") Integer id) {
        Org org = service.getById(id).getTarget();
        List<Org> orgList = service.getList(org).getList();
        setRequestAttr("org", org);
        setRequestAttr("orgList", orgList);
        setRequestAttr("categorys", dictionaryService.getList(new SysDictionary("ORG_CATEGORY")).getList());
        return "/admin/org/modify";
    }



    @ResponseBody
    @RequestMapping(value = "modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("superOrg") Integer superOrg,
            @RequestParam("image") MultipartFile image,
            @RequestParam("category") Integer category,
            @RequestParam("contacts") String contacts,
            @RequestParam("telephone") String telephone,
            @RequestParam("email") String email,
            @RequestParam("address") String address,
            @RequestParam("postcode") String postcode,
            @RequestParam("description") String description
    ) {
        Org org = service.getById(id).getTarget();
        org.setName(name);
        org.setHeadImageFile(image);
        org.setSuperOrg(new Org(superOrg));
        org.setCategory(new SysDictionary(category));
        org.setContacts(contacts);
        org.setTelephone(telephone);
        org.setEmail(email);
        org.setAddress(address);
        org.setPostcode(postcode);
        org.setDescription(description);
        Result<Org> result = service.modify(org, getRequest());
        result.setMessage(result.isSuccess() ? "更新成功" : result.getMessage());
        return  result.toString();
    }



    @RequestMapping("listV")
    public String listV(){
        return "admin/org/list";
    }


    @ResponseBody
    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "superOrg", required = false) Integer superOrg,
            @RequestParam(value = "category", required = false) Integer category,
            @RequestParam(value = "contacts", required = false) String contacts
    ) {
        PageResult<Org> pageResult = new PageResult<Org>();
        pageResult.setPage(page);
        pageResult.setSize(size);
        Org org = new Org();
        if (id!=null){
            org.setId(id);
        }
        if (StringUtils.hasLength(name)) {
            org.setName(name);
        }

        if (StringUtils.hasLength(contacts)) {
            org.setContacts(contacts);
        }

        if (category!=null){
            org.setCategory(new SysDictionary(category));
        }



        if (superOrg != null) {
            org.setSuperOrg(new Org(superOrg));
        }

        if (StringUtils.hasLength(order)) {
            pageResult.setSortBy(order);
            pageResult.setSortColumn("org.name");
        }
        pageResult.setT(org);
        Result<Org> result = service.getByPage(pageResult);
        setRequestAttr("result", result);
        result.setMessage(result.isSuccess() ? "更新成功" : result.getMessage());
        return  result.getPageResult().toString();
    }


    @ResponseBody
    @RequestMapping("audit")
    public String audit(
            @RequestParam("orgs[]") Integer [] orgs,
            @RequestParam("auditStatus") Integer auditStatus
    ){
        Result<Org> result=service.audit(orgs,auditStatus);
        return  result.toString();
    }






}

